/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.model.geo.halfEdgeDataStructure.Face;
import br.ufmg.dees.insane.model.geo.halfEdgeDataStructure.HalfEdge;
import br.ufmg.dees.insane.model.geo.halfEdgeDataStructure.Loop;
import br.ufmg.dees.insane.model.geo.halfEdgeDataStructure.Vertex;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;

/**
 * A class representing creation dialog on screen.<br>
 * @author  Penna, Samuel & Pitangueira, Roque
 * @see xyplot_view.InterfaceXYPlot
 * @since  april 2005
 */
public class InformationDialog extends JFrame { 
	
	/**   */
	private static final long serialVersionUID = 1L;
	
	private DesktopPostp desktop;
	private InterfacePostp parent;
	private JInternalFrame intFrame;
	//private ViewState state;
	
	private ArrayList selection;
	
	private JTabbedPane jTabbedPane; 
	private JScrollPane scroll; 
	private JPanel mainPane; 
	
	private JTextArea textArea; 
	private JScrollPane scrollTextArea; 
	private JPanel panel; 
	
	private JButton closeButton;
	
	
	public InformationDialog(InterfacePostp parent,ViewState vs, ArrayList selection){
		super("Information");
		this.parent = parent;
		this.selection = selection;
		init(); 
		this.setVisible(true); 
	} 
	
	
	public void init(){
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		jTabbedPane = new JTabbedPane(); 
		scroll = new JScrollPane(); 
		mainPane = (JPanel)this.getContentPane(); 
		
		scroll.setViewportView(jTabbedPane); 
		mainPane.setLayout(null); 
		
		for(int i=0;i<selection.size();i++){
			this.initializeComponent(selection.get(i));
		}
		
		if(selection.size()==0){
			
			textArea = new JTextArea(); 
			textArea.setBackground(Color.black);
			textArea.setForeground(Color.GREEN);
			textArea.setCaretColor(Color.GREEN);
			textArea.setSelectedTextColor(Color.YELLOW);
			textArea.setSelectionColor(Color.LIGHT_GRAY);
			textArea.setEditable(false);
			scrollTextArea = new JScrollPane(); 
			scrollTextArea.setAutoscrolls(true);
			
			panel = new JPanel(); 
			
			String text = "\n";
			text+="*************************************************************************** \n\n"; 
			text+="                                          Nothing was selected! \n\n";
			text+="*************************************************************************** \n\n"; 
			text+="                             If you want to check some information, \n";
			text+="                                              select an entity!";
			
			textArea.setText(text);
			scrollTextArea.setViewportView(textArea); 
			panel.setLayout(new BorderLayout(0, 0)); 
			panel.add(scrollTextArea, BorderLayout.CENTER); 
			jTabbedPane.addTab("Warning!", panel);
		}
		
		closeButton = new JButton("Close");
		
		closeButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						close(event);
					}
				});
		
		addComponent(mainPane, scroll, 5,5,380,270); 
		addComponent(mainPane, closeButton, 160,285,80,25);
		
		this.setTitle("Information"); 
		this.setAlwaysOnTop(true);
		this.setResizable(false);
		this.setLocation(350,300); 
		this.setSize(new Dimension(400, 350)); 
	}
	
	private void initializeComponent(Object obj){ 
		
		if(obj instanceof Vertex){
			Vertex vertex = (Vertex)obj;
			textArea = new JTextArea(); 
			textArea.setBackground(Color.black);
			textArea.setForeground(Color.GREEN);
			textArea.setCaretColor(Color.GREEN);
			textArea.setSelectedTextColor(Color.YELLOW);
			textArea.setSelectionColor(Color.LIGHT_GRAY);
			textArea.setEditable(false);
			scrollTextArea = new JScrollPane(); 
			scrollTextArea.setAutoscrolls(true);
			
			panel = new JPanel(); 
			
			String text = "";
			text+="  "+"Node: "+vertex.getId()+"\n";
			text+="\n";
			text+="  "+"Coords: \n";
			text+="  "+"X: "+vertex.getCoords().x+"\n"+"  "+"Y: "+vertex.getCoords().y+"\n"+"  "+
			"Z: "+vertex.getCoords().z+"\n";
			text+="\n";
			text+="  "+"Values:\n";
			text+="  "+"Number of Values: "+(vertex.getKeys().size())+"\n";
			for(int i=0;i<vertex.getKeys().size();i++){
				text+="  "+vertex.getKeys().get(i)+": "+vertex.getValue((String)vertex.getKeys().get(i))+"\n";
			}
			
			textArea.setText(text);
			scrollTextArea.setViewportView(textArea); 
			panel.setLayout(new BorderLayout(0, 0)); 
			panel.add(scrollTextArea, BorderLayout.CENTER); 
			jTabbedPane.addTab("Node "+vertex.getId(), panel);
		}
		
		if(obj instanceof Face){
			Face face = (Face)obj;
			textArea = new JTextArea(); 
			textArea.setBackground(Color.black);
			textArea.setForeground(Color.GREEN);
			textArea.setCaretColor(Color.GREEN);
			textArea.setSelectedTextColor(Color.YELLOW);
			textArea.setSelectionColor(Color.LIGHT_GRAY);
			textArea.setEditable(false);
			scrollTextArea = new JScrollPane(); 
			scrollTextArea.setAutoscrolls(true);
			
			panel = new JPanel(); 
			
			String text = "";
			text+="  "+"Element: "+face.getId()+"\n";
			text+="\n";
			text+="  "+"Incidence: \n";
			text+="  "+"Nodes: ";
			Loop loop; 
			for(int i=0;i<face.getLoopList().size();i++){
				loop = (Loop)face.getLoopList().get(i);
				for(int j=0;j<loop.getHalfEdgeList().size();j++){
					HalfEdge he = (HalfEdge)loop.getHalfEdgeList().get(j);
					Vertex v = he.getVertex();
					text+=v.getId()+" ";
				}
			}
			text+="\n\n";
			for(int i=0;i<face.getLoopList().size();i++){
				loop = (Loop)face.getLoopList().get(i);
				for(int j=0;j<loop.getHalfEdgeList().size();j++){
					HalfEdge he = (HalfEdge)loop.getHalfEdgeList().get(j);
					Vertex v = he.getVertex();
					text+="  "+"************************************\n";
					text+="  "+"Node "+v.getId()+"\n";
					text+="  "+"Coords: ";
					text+="X: "+v.getCoords().x+"  ";
					text+="Y: "+v.getCoords().y+"  ";
					text+="Z: "+v.getCoords().z+"  ";
					text+="  "+"\n";
					for(int k=0;k<v.getKeys().size();k++){
						text+="  "+v.getKeys().get(k)+": "+v.getValue((String)v.getKeys().get(k))+"\n";
					}
				}
				/* for(int j=0;j<face.getInternalVertex().size();j++){
				 Vertex iV = (Vertex)face.getInternalVertex().get(j);
				 text+="  "+"************************************\n";
				 text+="  "+"Node "+iV.getId()+"\n";
				 text+="  "+"Coords: ";
				 text+="X: "+iV.getCoords().x+"  ";
				 text+="Y: "+iV.getCoords().y+"  ";
				 text+="Z: "+iV.getCoords().z+"  ";
				 text+="  "+"\n";
				 for(int k=0;k<iV.getKeys().size()-1;k++){
				 text+="  "+iV.getKeys().get(k)+": "+iV.getValue((String)iV.getKeys().get(k))+"\n";
				 }
				 }
				 */
			}
			
			textArea.setText(text);
			scrollTextArea.setViewportView(textArea); 
			panel.setLayout(new BorderLayout(0, 0)); 
			panel.add(scrollTextArea, BorderLayout.CENTER); 
			jTabbedPane.addTab("Element "+face.getId(), panel);
			
		}
		
		
		
	} 
	
	/** Add Component Without a Layout Manager (Absolute Positioning) */ 
	private void addComponent(Container container,Component c,int x,int y,int width,int height) { 
		c.setBounds(x,y,width,height); 
		container.add(c); 
	} 
	
	public void close(ActionEvent e){
		((InterfacePostp)this.parent).getView().getController().getSelection().clear();
		((InterfacePostp)this.parent).getView().getController().compose();
		this.dispose();
	}
	
	public DesktopPostp getDesktop() {
		return desktop;
	}
	
	public void setDesktop(DesktopPostp desktop) {
		this.desktop = desktop;
	}
	
	public JInternalFrame getIntFrame() {
		return intFrame;
	}
	
	public void setIntFrame(JInternalFrame intFrame) {
		this.intFrame = intFrame;
	}
	
	public ArrayList getSelection() {
		return selection;
	}
	
	public void setSelection(ArrayList selection) {
		this.selection = selection;
	} 
//	***********************************************************************************	
}//
